package Exp_3;

public class LinkQueue implements IQueue {
    private Node front;//队首指针
    private Node rear;//队尾指针

    //链队列的构造函数
    public LinkQueue() {
        front = rear = null;
    }


    public void clear() {
        front = rear = null;

    }

    public int length() {
        Node p = front;
        int length = 0;
        while (p != null) {
            p = p.next;
            ++length;
        }
        return length;
    }

    public boolean isEmpty() {

        return front == null;
    }

    //返回队首元素
    public Object peek() {
        if (front != null)
            return front.data;
        else
            return null;
    }

    public void offer(Object x) throws Exception {
        Node p = new Node(x);
        if (front != null) {
            rear.next = p;
            rear = p;
        } else
            front = rear = p;

    }

    public Object pool() {
        Node p = new Node();
        if (front != null) {
            p = front;
            front = front.next;
            if (p == rear)
                rear = null;
            return p.data;
        } else
            return null;
    }


}
